package com.dwxt.parabolic.service;

import com.dwxt.common.base.BaseResult;
import com.dwxt.parabolic.base.LevelType;
import com.dwxt.parabolic.dao.AreaMapper;
import com.dwxt.parabolic.entity.Area;
import com.dwxt.parabolic.entity.AreaExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AreaService {
    @Autowired
    private AreaMapper areaMapper;

    public BaseResult getOneLevelArea() {
        List<Area> areaList = selectByLevelType(LevelType.ONE);
        return BaseResult.ok(areaList);
    }

    /**
     * 根据levelType 查询Area
     *
     * @param levelType
     * @return
     */
    public List<Area> selectByLevelType(LevelType levelType) {
        AreaExample example = new AreaExample();
        example.setOrderByClause("id");
        example.createCriteria().andLevelTypeEqualTo(levelType.getLevel());
        return areaMapper.selectByExample(example);
    }

    public BaseResult getByParentId(String parentId) {
        List<Area> areaList = selectByParentId(parentId);
        return BaseResult.ok(areaList);
    }

    /**
     * 根据parentId 查询 Area
     *
     * @param parentId
     * @return
     */
    public List<Area> selectByParentId(String parentId) {
        AreaExample example = new AreaExample();
        example.setOrderByClause("id");
        example.createCriteria().andParentIdEqualTo(parentId);
        return areaMapper.selectByExample(example);
    }

    public Area selectByPrimaryKey(String id) {
        return areaMapper.selectByPrimaryKey(id);
    }
}
